using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SiteManager;
using System.IO;

public partial class includes_PremiumFeaturedDentist : System.Web.UI.UserControl
{
    Manager mgr = new Manager(true,"ConnectionString");
    protected localFunction lfunction = new localFunction();
    public string frompage = "";
    public string PreDoctorsId = "";
    string Featop3 = "";
    public string TravelDis = "";
    int pageNo = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        bool PreFeaDocsFound = false;


        try
        {
            pageNo = Convert.ToInt32(Request.QueryString["page"]);
            if (pageNo == 0)
                pageNo = 1;
        }
        catch
        {
            pageNo = 1;
        }
        if (!Page.IsPostBack)
        {
            PreFeaDocsFound = (Session["PreDoctorsId"] != null && Session["PreDoctorsId"].ToString().Trim() != "");

            if (Session["Travel_Dis"] != null && Session["Travel_Dis"].ToString().Trim() != "")
            {
                TravelDis = Session["Travel_Dis"].ToString();
            }

            if (PreFeaDocsFound)
            {
                //string to store all the doctor id's

                PreDoctorsId = Session["PreDoctorsId"].ToString();


                try
                {
                    frompage = Session["pageFromOrAction"].ToString();

                }
                catch
                {
                    //frompage = "advanced";  
                }

                if (frompage == "advanced")
                {
                    BindGridPreFeaDoc();

                }
                else if (frompage == "connect") //When the page is redirect from doctorContent.aspx
                {
                    BindGridPreFeaDoc();

                }
                else
                {
                    BindGridPreFeaDoc();

                }
            }
            else
            {
                //Response.Redirect("SearchDentist_adv.aspx");
            }
        }

    }
    string returnSql(string ids)
    {
        Manager mgr = new Manager(true,"ConnectionString");
        string strSql = @"SELECT QU FROM DENTIST_FUNC_RATING()";

        SqlDataReader dr = mgr.ExecuteQuery(strSql);
        System.String orderClause = "";
        System.String AttachMiles = "";
        if (Session["Travel_Dis"].ToString() != "" && Session["Travel_Dis"].ToString() != null) //  to order the result first by Distance and then by rating Desc / to Calculate Miles
        {
            orderClause = "order by Distance ASC, Rating Desc";
            AttachMiles = " , (dbo.GetDistanceInMiles('" + Session["ZipCode"].ToString() + "','" + Session["City"].ToString() + "','" + Session["State"].ToString() + "',zipcodeforsearch)) as Distance";

        }
        else
        {
            orderClause = "order by Rating Desc";
            AttachMiles = ",'' as distance";

        }

       if (dr.Read())
       {
            strSql = @"select (" + dr[0].ToString()
                 + @") as rating,d.id as id,fname + ' ' + mname + ' ' + lname as docname, address1 + '<br>' + city + ', ' + state as address,"
             + "Sp.specialty + isnull(', ' + (select tbl_DentistSubSpecialty.specialty from tbl_DentistSubSpecialty where"
            + " tbl_DentistSubSpecialty.specialty not in(Sp.specialty) and "
             + " tbl_DentistSubSpecialty.id=d.secondaryspecialityid),'') as speciality,"
            + " isnull(dp.website,'')as website,isnull(dp.photo,'') as photo,d.gender "
            + AttachMiles
            + " from tbl_Dentists d "
            + " left outer join tbl_secondaryspeciality s on s.id=d.secondaryspecialityid "
            + " left outer join tbl_dntpremium dp on dp.dnt_id=d.id "
            + " left outer join tbl_dentistspecialty sp on sp.id=D.specialityid "            
             + " where  d.isdelete=0 and d.id in (" + ids + ") " + orderClause;//ORDER BY RATING DESC";

            //a.d. code, will be implemented, if required

            //(dbo.GetDistanceInMiles('" + Session["Travel_Dis"].ToString() + "','" + Session["City"].ToString() 
            //+ @"',substring(d.ZipCode,1,5))) as Distance

        }

        return strSql;
    }
    public void BindGridPreFeaDoc()
    {

        Featop3 = Session["PreDoctorsId"].ToString();

        if (Featop3.Length > 0)
        {
            try
            {

                DataSet ds = mgr.ExecuteQuery(returnSql(Featop3), "tbl_drdet");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (Session["Travel_Dis"].ToString() != "" && Session["Travel_Dis"].ToString() != null)  // to make distance column visibility true if search criteria includes Distance Search Functionality
                    {
                        ds.Tables[0].DefaultView.Sort = " distance,rating desc";
                        grd_top3.Columns[4].Visible = true;
                    }
                    else
                    {
                        ds.Tables[0].DefaultView.Sort = " rating desc";
                        grd_top3.Columns[4].Visible = false;

                    }
                    if(ds.Tables[0].Rows.Count>0)
                    {

                    grd_top3.DataSource = ds.Tables[0].DefaultView;

                    grd_top3.DataBind();
                    }

                    lblMsgTop3.Text = "";
                    lblMsgTop3.ForeColor = System.Drawing.Color.Black;
                    lblMsgTop3.Font.Bold = true;
                    lblMsgTop3.Font.Size = 8;

                }
                else
                {
                    //td_fealinks.Visible = false;
                    //lbl_FeaMsg.Text = "No Featured Links";
                    //lbl_FeaMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        else
        {
            //td_fealinks.Visible = false;
            //lblMsgTop3.Text = "No Featured Links";
            //lblMsgTop3.ForeColor = System.Drawing.Color.Red;

        }
    }

    protected void grdSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        double overallRate = 0.0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            overallRate = Convert.ToDouble(e.Row.Cells[5].Text);
            if (overallRate > 100)
                overallRate = 100;

            ///K: 12 oct 2006
            ///if the doctors are rated below 50 they should be labeled as 'Not Rated'
            float fRateLimit = Convert.ToSingle(ConfigurationManager.AppSettings["MinRatingLimit"].ToString());
            e.Row.Cells[5].Text = ((overallRate >= fRateLimit) ? Math.Round(overallRate, 2).ToString() : "Not Rated");
            e.Row.Cells[5].ForeColor = ((overallRate >= fRateLimit) ? System.Drawing.Color.Blue : System.Drawing.Color.Red);
            ///k: code ends

        }
        if (e.Row.RowType == DataControlRowType.Pager)
        {

        }
    }
    public string getPhoto(object photo, object sex)
    {
        string p = photo.ToString();
        if (p.Length > 0)
        {
            if (File.Exists(Request.PhysicalApplicationPath + "\\images\\Dr_images\\" + photo))
            {
                //return "<img height=\"100\" width=\"80\" src='images/Dr_images/" + photo + "'>";
                string str_path = mgr.getThumbnaleImage("images/Dr_images/" + photo, 80, 100, false, "");
                return str_path;
            }
            else
            {
                if (sex.ToString().ToLower() == "m")
                    return "<img height=\"100\" width=\"80\" src='images/Dr_images/silhouette-male.gif'>";
                else
                    return "<img height=\"100\" width=\"80\" src='images/Dr_images/silhouette--female.gif'>";
            }
        }
        else
        {
            if (sex.ToString().ToLower() == "m")
                return "<img height=\"100\" width=\"80\" src='images/Dr_images/silhouette-male.gif'>";
            else
                return "<img height=\"100\" width=\"80\" src='images/Dr_images/silhouette--female.gif'>";
        }
    }
}
